package vip.specimen.system.service.question;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import vip.specimen.system.common.enums.QuestionTypeEnum;
import vip.specimen.system.datasource.entity.question.QuestionProblemEntity;
import vip.specimen.system.datasource.validation.question.QuestionProblemValidation;
import vip.specimen.system.repository.question.ProblemRepository;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.DataBaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/4/22 17:39
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
public class ProblemService {
    @Resource
    private ProblemRepository problemRepository;
    @Resource
    private ColumnService columnService;
    @Resource
    private HttpServletRequest request;

    /**
     * 新增问题
     *
     * @param questionProblemValidation 验证
     * @return 实体
     */
    public QuestionProblemEntity insert(QuestionProblemValidation questionProblemValidation) {
        QuestionProblemEntity problemEntity = new QuestionProblemEntity(
                questionProblemValidation.getProblem(), questionProblemValidation.getResult(), questionProblemValidation.getAnalysis(),
                questionProblemValidation.getClick(), questionProblemValidation.getIsPay(), questionProblemValidation.getPay(),
                QuestionTypeEnum.valueOf(questionProblemValidation.getQuestionType()), this.columnService.findById(questionProblemValidation.getCategories())
        );
        return this.problemRepository.save(problemEntity);
    }

    /**
     * 分页查询
     *
     * @return Page 对象
     */
    public Page<QuestionProblemEntity> findAllPage() {
        JSONObject param = CommonUtil.pageParams(this.request);
        return this.problemRepository.findAll(DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime")));
    }

    /**
     * 更新状态
     *
     * @param uuid uuid
     * @return 实体
     */
    public QuestionProblemEntity update(UUID uuid) {
        if (this.problemRepository.existsById(uuid)) {
            Optional<QuestionProblemEntity> o = this.problemRepository.findById(uuid);
            if (!o.isPresent()) return null;
            QuestionProblemEntity problemEntity = o.get();
            problemEntity.setEnable(!problemEntity.isEnable());
            problemEntity.setUpdateTime(Calendar.getInstance().getTime());
            this.problemRepository.save(problemEntity);
            return problemEntity;
        }
        return null;
    }

    /**
     * 删除一个
     *
     * @param uuid uuid
     * @return 是否成功
     */
    public boolean delete(UUID uuid) {
        if (this.problemRepository.existsById(uuid)) {
            this.problemRepository.deleteById(uuid);
            return true;
        }
        return false;
    }

    /**
     * 批量删除
     *
     * @param uuids uuid列表
     * @return 成功数
     */
    public int delete(Set<UUID> uuids) {
        int successNum = 0;
        for (UUID uuid : uuids) {
            successNum += this.delete(uuid) ? 1 : 0;
        }
        return successNum;
    }

    /**
     * 更新问题
     *
     * @param uuid                      uuid
     * @param questionProblemValidation 验证
     * @return 实体
     */
    public QuestionProblemEntity update(UUID uuid, QuestionProblemValidation questionProblemValidation) {
        if (null != uuid && this.problemRepository.existsById(uuid)) {
            Optional<QuestionProblemEntity> o = this.problemRepository.findById(uuid);
            if (!o.isPresent()) return null;
            QuestionProblemEntity problemEntity = o.get();
            problemEntity.setAnalysis(questionProblemValidation.getAnalysis());
            problemEntity.setCategories(this.columnService.findById(questionProblemValidation.getCategories()));
            problemEntity.setClick(questionProblemValidation.getClick());
            problemEntity.setIsPay(questionProblemValidation.getIsPay());
            problemEntity.setPay(questionProblemValidation.getPay());
            problemEntity.setProblem(questionProblemValidation.getProblem());
            problemEntity.setResult(questionProblemValidation.getResult());
            problemEntity.setQuestionType(QuestionTypeEnum.valueOf(questionProblemValidation.getQuestionType()));
            problemEntity.setUpdateTime(Calendar.getInstance().getTime());
            return this.problemRepository.save(problemEntity);
        }
        return null;
    }

    /**
     * 根据问题查询
     *
     * @param problem 问题
     * @return 实体
     */
    public QuestionProblemEntity findByProblem(String problem) {
        return this.problemRepository.findByProblem(problem);
    }
}
